import { ref } from 'vue'

import { shareAppList, curShareAppIdx } from '../data'
import { downloadImage } from '@/libs/file-utils'
import { topMessage } from '@/components/TopMessage/data'
import { makeErrMsg } from '@/libs/message'
import { catchFn } from '@/libs/common'

const showShareMask = ref(false)

export { showShareMask }

const copy = () => {
  const item = shareAppList[curShareAppIdx.value]

  uni.setClipboardData({
    data: item.url,
    success: () => (showShareMask.value = false),
    fail: err => topMessage.error(makeErrMsg(err)),
  })
}

const download = () =>
  catchFn(async () => {
    const item = shareAppList[curShareAppIdx.value]

    await downloadImage(item.qrcode)

    showShareMask.value = false
  })

export { copy, download }
